﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Collections;
using MM.Common;
using MM.DBUtility;

namespace JobUpdateNextTime
{
    public partial class Form1 : Form
    {
        private clsLogTool _log = new clsLogTool();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _log.LogFilePath = Directory.GetParent(Assembly.GetExecutingAssembly().Location) + "\\LOG\\";
            _log.LogFileName = "ErrorLog.txt";

            try
            {
                _log.WriteLog("============================JobStart==============================");
                DoJob();
                _log.WriteLog("Successful================JobEnd==============================");
            }
            catch (Exception ex)
            {
                _log.WriteLog(ex.ToString());
                _log.WriteLog("Failed====================JobEnd==============================");
            }
            this.Close();
        }

        private void DoJob()
        {
            DataTable dt = DbHelperSql.Query(string.Format("SELECT ID,TimeInterval,NextTime FROM T_Rules WHERE NextTime='{0}'", DateTime.Now.ToString("yyyy-MM-dd"))).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                ArrayList alist = new ArrayList();
                foreach (DataRow row in dt.Rows)
                {
                    alist.Add(string.Format("UPDATE T_Rules SET NextTime='{0}' WHERE ID={1}", Convert.ToDateTime(row["NextTime"].ToString()).AddDays(Convert.ToDouble(row["TimeInterval"].ToString())).ToString("yyyy-MM-dd"), row["ID"].ToString()));
                }
                MM.DBUtility.DbHelperSql.ExecuteSqlTran(alist);
            }
            else
                _log.WriteLog("Do Nothing...");
        }
    }
}
